package com.crack.nga2.activity.userCenter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.crack.nga2.R;
import com.crack.nga2.activity.FloorActivity;
import com.crack.nga2.custom.PullListView;
import com.crack.nga2.entity.Theme;
import com.crack.nga2.utils.GlobalConst;
import com.crack.nga2.utils.TimeUtils;
import com.crack.nga2.utils.HttpParams;
import com.crack.nga2.volley.UserThemeRequest;


import java.util.ArrayList;
import java.util.List;

public class UserThemeActivity extends AppCompatActivity implements PullListView.OnRefreshListener, AdapterView.OnItemClickListener {
    /*package*/ PullListView listView;
    /*package*/ ThemeAdapter adapter = new ThemeAdapter();
    /*package*/ List<Theme> themes = new ArrayList<>();
    /*package*/ int page = 1;
    /*package*/ TextView title;
    /*package*/ RequestQueue mQueue;
    private int uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_theme);
        uid = getIntent().getIntExtra("uid",-1);
        mQueue = Volley.newRequestQueue(this);
        title = (TextView) findViewById(R.id.action_title);
        listView = (PullListView) findViewById(R.id.listView);
        listView.setNoPullUp(true);
        listView.setAdapter(adapter);
        listView.setOnRefreshListener(this);
        listView.setOnItemClickListener(this);
        listView.onStartHeadRefresh();
        getData(page);
    }

    protected HttpParams getParams() {
        HttpParams params = new HttpParams();
        if(uid!=-1){
            params.put("authorid",uid);
        }else {
            params.put("authorid", GlobalConst.UID);
        }
        params.put("page", page);
        params.put("lite", "js");
        return params;
    }

    /*package*/ void getData(final int page) {
        String url = GlobalConst.BASE_URL + "thread.php?" + getParams().toString();
        mQueue.add(new UserThemeRequest(Request.Method.GET, url,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        listView.onCompleteRefresh();
                    }
                },
                new Response.Listener<List<Theme>>() {
                    @Override
                    public void onResponse(List<Theme> response) {
                        if (page == 1) {
                            themes.clear();
                        }
                        themes.addAll(response);
                        adapter.notifyDataSetChanged();
                        listView.onCompleteRefresh();
                    }
                }));

    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.action_return:
                finish();
                break;
        }
    }

    @Override
    public void pullDownToRefresh() {
        page = 1;
        getData(page);
    }

    @Override
    public void pullUpToRefresh() {
        page++;
        getData(page);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, FloorActivity.class);
        intent.putExtra("theme", id);
        startActivity(intent);
    }

    private class ThemeAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return themes.size();
        }

        @Override
        public Object getItem(int position) {
            return themes.get(position);
        }

        @Override
        public long getItemId(int position) {
            return themes.get(position).getTid();
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            Theme theme = themes.get(position);
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(UserThemeActivity.this, R.layout.activity_forum_theme_item, null);
                holder = new ViewHolder();
                holder.title = (TextView) view.findViewById(R.id.post_title);
                holder.username = (TextView) view.findViewById(R.id.post_user_name);
                holder.last_reply_time = (TextView) view.findViewById(R.id.post_reply_time);
                holder.reply_count = (TextView) view.findViewById(R.id.post_reply_count);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            holder.title.setText(Html.fromHtml(theme.getSubject()));
            holder.username.setText(theme.getAuthor());
            holder.last_reply_time.setText(TimeUtils.format(theme.getLastpost() * 1000));
            holder.reply_count.setText(String.valueOf(theme.getReplies()));

            return view;
        }
    }

    class ViewHolder {
        TextView title;
        TextView username;
        TextView last_reply_time;
        TextView reply_count;
    }
}
